<?php
namespace Admin\Controller;
use Think\Controller;
//游戏盒子
class GameBoxController extends CommonController {

    public function index(){
        self::game_list();
    }


    public function game_list()
    {
        //暂时不做分页
        $field              =   array('boxes_app_id','logo','appid','url','envVersion','content','title','isshow','category','h5show','sort');
        $data['game_list']  =   M('boxes_app')->field($field)->order('sort asc')->select();
        $this->assign("data",$data);
        $this->display('game_list');
    }

    /**
     * 小说
     */
    public function game_fiction()
    {
        //暂时不做分页
        $field              =   array('id','logo','appid','url','envVersion','content','title','isshow','category','h5show','sort');
        $data['game_list']  =   M('boxes_fiction')->field($field)->order('sort asc')->select();
        $this->assign("data",$data);
        $this->display('game_fiction');
    }

    public function game_fiction_edit()
    {
        if (IS_AJAX)
        {
            $where['id']  =   I('post.id','','htmlspecialchars');
            if (empty($where['id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');

            $data['url']       =   $_POST['url'];

            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            //$data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['category']  =   I('post.category','','htmlspecialchars');
            $data['h5show']    =   I('post.h5show','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');
            $data['sort']     =   I('post.sort','','htmlspecialchars');

            if ($_FILES){
                $setting= C('UPLOAD_SITEIMG_QINIU');
                $setting['savePath'] = 'appshow/';
                $Upload = new \Think\Upload($setting);
                $info = $Upload->upload($_FILES);
                $file = $info['logo'];
                $data['logo']      =   $file['name'];

                if ($data['logo']){

                    $headimg=M('boxes_fiction')->where($where)->getField('logo');

                    $Qiniu = new \Think\Upload\Driver\Qiniu\QiniuStorage($setting['driverConfig']);

                    $result = $Qiniu->del($headimg);

                    $error = $Qiniu->errorStr;//错误信息

                }

            }

            $nowsort = M('boxes_fiction')->where($where)->getField('sort');

            if ($data['sort']!=$nowsort){  //更新的排序不等于原来的排序
                $where1['sort'] = array('egt',$data['sort']);
                $ids = M('boxes_fiction')->where($where1)->getField('id',true);

                if (!empty($ids)){
                    $ids = array_merge($ids);

                    for ($i=0;$i<count($ids);$i++){

                        M('boxes_fiction')->where(array('id'=>$ids[$i]))->setInc('sort');  //后面的排序自增
                    }
                }
            }



            $result     =   M('boxes_fiction')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['id']))
            {
                $this->error('非法请求');
            }

            $field              =   array('id','logo','appid','url','envVersion','content','title','isshow','category','h5show','sort');
            $data['game_info']  =   M('boxes_fiction')->where($where)->field($field)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_fiction_edit');
        }
    }

    public function game_fiction_add()
    {
        if (IS_AJAX)
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   $_POST['url'];
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            //$data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['category']  =   I('post.category','','htmlspecialchars');
            $data['h5show']    =   I('post.h5show','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');
            $data['sort']     =   I('post.sort','','htmlspecialchars');

            $where['sort'] = array('egt',$data['sort']);

            if ($_FILES){

                $setting= C('UPLOAD_SITEIMG_QINIU');
                $setting['savePath'] = 'appshow/';
                $Upload = new \Think\Upload($setting);
                $info = $Upload->upload($_FILES);
                $file = $info['logo'];
                $data['logo']      =   $file['name'];

            }

            $ids = M('boxes_fiction')->where($where)->getField('id',true);

            if (!empty($ids)){
                $ids = array_merge($ids);

                for ($i=0;$i<count($ids);$i++){
                    M('boxes_fiction')->where(array('id'=>$ids[$i]))->setInc('sort');
                }
            }

            $result     =   M('boxes_fiction')->add($data);



            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新添加';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_fiction_add');
        }
    }



    /**
     * banner
     */
    public function game_banner()
    {

        $setting= C('UPLOAD_SITEIMG_QINIU');

        //暂时不做分页
        $field              =   array('boxes_headapp_id','headimg','appid','url','envVersion','headisshow','category','h5show','title','sort');
        $data['game_list']  =   M('boxes_headapp')->field($field)->order('sort asc')->select();
        $this->assign("data",$data);
        $this->display('game_banner');
    }


    public function game_banner_edit(){

        if (IS_AJAX)
        {
            $where['boxes_headapp_id']  =   I('post.boxes_headapp_id','','htmlspecialchars');
            if (empty($where['boxes_headapp_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   $_POST['url'];
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');

            $data['headisshow']    =   I('post.headisshow','','htmlspecialchars');
            $data['category']  =   I('post.category','','htmlspecialchars');
            $data['h5show']    =   I('post.h5show','','htmlspecialchars');
            $data['title']    =   I('post.title','','htmlspecialchars');
            $data['sort']    =   I('post.sort','','htmlspecialchars');

            if ($_FILES){
                $setting= C('UPLOAD_SITEIMG_QINIU');
                $setting['savePath'] = 'appshow/';
                $Upload = new \Think\Upload($setting);
                $info = $Upload->upload($_FILES);
                $file = $info['headimg'];
                $data['headimg']      =   $file['name'];

                if ($data['headimg']){

                    $headimg=M('boxes_headapp')->where($where)->getField('headimg');

                    $Qiniu = new \Think\Upload\Driver\Qiniu\QiniuStorage($setting['driverConfig']);

                    $result = $Qiniu->del($headimg);

                    $error = $Qiniu->errorStr;//错误信息

                }

            }


            $nowsort = M('boxes_headapp')->where($where)->getField('sort');

            if ($data['sort']!=$nowsort){  //更新的排序不等于原来的排序
                $where1['sort'] = array('egt',$data['sort']);
                $ids = M('boxes_headapp')->where($where1)->getField('boxes_headapp_id',true);

                if (!empty($ids)){
                    $ids = array_merge($ids);

                    for ($i=0;$i<count($ids);$i++){

                        M('boxes_headapp')->where(array('boxes_headapp_id'=>$ids[$i]))->setInc('sort');  //后面的排序自增
                    }
                }
            }


            $result     =   M('boxes_headapp')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['boxes_headapp_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['boxes_headapp_id']))
            {
                $this->error('非法请求');
            }

            $field              =    array('boxes_headapp_id','headimg','appid','url','envVersion','headisshow','category','h5show','title','sort');
            $data['game_info']  =   M('boxes_headapp')->where($where)->field($field)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_banner_edit');
        }

    }


    public function game_banner_add()
    {

        if (IS_AJAX)
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   $_POST['url'];
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');

            $data['headisshow']    =   I('post.headisshow','','htmlspecialchars');
            $data['category']  =   I('post.category','','htmlspecialchars');
            $data['h5show']    =   I('post.h5show','','htmlspecialchars');
            $data['title']    =   I('post.title','','htmlspecialchars');
            $data['sort']    =   I('post.sort','','htmlspecialchars');

            $where['sort'] = array('egt',$data['sort']);

            if ($_FILES){

            $setting= C('UPLOAD_SITEIMG_QINIU');
            $setting['savePath'] = 'appshow/';
            $Upload = new \Think\Upload($setting);
            $info = $Upload->upload($_FILES);
            $file = $info['headimg'];
            $data['headimg']      =   $file['name'];

        }

            $ids = M('boxes_headapp')->where($where)->getField('boxes_headapp_id',true);

            if (!empty($ids)){
                $ids = array_merge($ids);

                for ($i=0;$i<count($ids);$i++){
                    M('boxes_headapp')->where(array('boxes_headapp_id'=>$ids[$i]))->setInc('sort');
                }
            }



            $result     =   M('boxes_headapp')->add($data);

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新添加';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_banner_add');
        }
    }

    public function game_more_edit()
    {
        if (IS_AJAX)
        {
            $where['boxes_app_id']  =   I('post.boxes_app_id','','htmlspecialchars');
            if (empty($where['boxes_app_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   $_POST['url'];
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            //$data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['category']  =   I('post.category','','htmlspecialchars');
            $data['h5show']    =   I('post.h5show','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');
            $data['sort']     =   I('post.sort','','htmlspecialchars');

            if ($_FILES){
                $setting= C('UPLOAD_SITEIMG_QINIU');
                $setting['savePath'] = 'appshow/';
                $Upload = new \Think\Upload($setting);
                $info = $Upload->upload($_FILES);
                $file = $info['logo'];
                $data['logo']      =   $file['name'];

                if ($data['logo']){

                    $headimg=M('boxes_app')->where($where)->getField('logo');

                    $Qiniu = new \Think\Upload\Driver\Qiniu\QiniuStorage($setting['driverConfig']);

                    $result = $Qiniu->del($headimg);

                    $error = $Qiniu->errorStr;//错误信息

                }

            }

            $nowsort = M('boxes_app')->where($where)->getField('sort');

            if ($data['sort']!=$nowsort){  //更新的排序不等于原来的排序
                $where1['sort'] = array('egt',$data['sort']);
                $ids = M('boxes_app')->where($where1)->getField('boxes_app_id',true);

                if (!empty($ids)){
                    $ids = array_merge($ids);

                    for ($i=0;$i<count($ids);$i++){

                            M('boxes_app')->where(array('boxes_app_id'=>$ids[$i]))->setInc('sort');  //后面的排序自增
                    }
                }
            }



            $result     =   M('boxes_app')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['boxes_app_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['boxes_app_id']))
            {
                $this->error('非法请求');
            }

            $field              =   array('boxes_app_id','logo','appid','url','envVersion','content','title','isshow','category','h5show','sort');
            $data['game_info']  =   M('boxes_app')->where($where)->field($field)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_more_edit');
        }
    }

    public function game_more_add()
    {
        if (IS_AJAX)
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   $_POST['url'];
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            //$data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['category']  =   I('post.category','','htmlspecialchars');
            $data['h5show']    =   I('post.h5show','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');
            $data['sort']     =   I('post.sort','','htmlspecialchars');

            $where['sort'] = array('egt',$data['sort']);

            if ($_FILES){

                $setting= C('UPLOAD_SITEIMG_QINIU');
                $setting['savePath'] = 'appshow/';
                $Upload = new \Think\Upload($setting);
                $info = $Upload->upload($_FILES);
                $file = $info['logo'];
                $data['logo']      =   $file['name'];

            }

            $ids = M('boxes_app')->where($where)->getField('boxes_app_id',true);

            if (!empty($ids)){
                $ids = array_merge($ids);

                for ($i=0;$i<count($ids);$i++){
                    M('boxes_app')->where(array('boxes_app_id'=>$ids[$i]))->setInc('sort');
                }
            }

            $result     =   M('boxes_app')->add($data);



            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新添加';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_more_add');
        }
    }

}